package com.lzb.musicplayer.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.basic.dialog.BaseSmartDialog;
import com.lzb.musicplayer.R;
import com.lzb.musicplayer.adapter.AddPlaylistAdapter;
import com.lzb.musicplayer.bean.MusicFile;
import com.lzb.musicplayer.bean.PlayListBean;
import com.lzb.musicplayer.databinding.DialogAddPlaylistBinding;
import com.lzb.musicplayer.databinding.DialogPlaylistBinding;
import com.lzb.musicplayer.utils.AppSpUtils;

import java.util.List;


public class PlaylistDialog extends BaseSmartDialog<DialogPlaylistBinding> implements View.OnClickListener {

    private List<MusicFile> musicFileList;
    private AddPlaylistAdapter playlistAdapter;

    public void setMusicFileList(List<MusicFile> musicFileList) {
        this.musicFileList = musicFileList;
    }

    public PlaylistDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_playlist;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        mDataBinding.rvPlayList.setLayoutManager(new LinearLayoutManager(getContext()));
        playlistAdapter = new AddPlaylistAdapter();
        mDataBinding.rvPlayList.setAdapter(playlistAdapter);

        playlistAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<PlayListBean> listSp = AppSpUtils.getPlayList();
                listSp.get(position).getMusicFileList().addAll(musicFileList);
                AppSpUtils.savePlayList(listSp);
                dismiss();
            }
        });

        getData();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            showAddPlaylistDialog();
        }
    }

    private void getData() {
        List<PlayListBean> list = AppSpUtils.getPlayList();
        if (list != null && list.size() > 0) {
            playlistAdapter.setList(list);
            mDataBinding.rvPlayList.setVisibility(View.VISIBLE);
            mDataBinding.tvPlaylistNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvPlayList.setVisibility(View.GONE);
            mDataBinding.tvPlaylistNoData.setVisibility(View.VISIBLE);
        }
    }

    private void showAddPlaylistDialog() {
        AddPlaylistDialog dialog = new AddPlaylistDialog(getContext());
        dialog.setListener(new AddPlaylistDialog.Ilistener() {
            @Override
            public void clickConfirm() {
                getData();
            }
        });
        dialog.show();
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConItem();
    }
}
